/* Copyright (C) 2012  	Mathieu Marleix <mathieu.marleix@aerian.fr>
 * 						Sébastien Dufay
 * 						Romain Noel
 * 						Nicolas Dennewald
 * 						Slim Aloui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.esiee.mobiliteam;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.widget.Toast;

/**
 * Gestion de l'action provoquée au déclenchement de l'alarme.
 * @author Mathieu Marleix
 * @author Sébastien Dufay
 * @author Romain Noel
 * @author Nicolas Dennewald
 * @author Slim Aloui
 */
public class AlarmReceiver extends BroadcastReceiver{
	AlertDialog alertDialog = null;
	@Override
	public void onReceive(Context context, Intent intent) {
		try {
			//			Toast.makeText(context, "Il est l'heure en cours!",Toast.LENGTH_LONG).show();
			alertDialog = new AlertDialog.Builder(context).create();
			alertDialog.setTitle(context.getString(R.string.alarm_title));
			alertDialog.setMessage(context.getString(R.string.alarm_text));

			alertDialog.setButton(context.getString(R.string.ok),new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					alertDialog.dismiss();

				} }); 
		} catch (Exception r) {
			Toast.makeText(context, "Error",Toast.LENGTH_SHORT).show();
			r.printStackTrace();
		}		
		Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
		
		final MediaPlayer mp = MediaPlayer.create(context, R.raw.alarm);
		mp.start();
		Handler h = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				if(msg.what==0){
					mp.stop();
				}
				super.handleMessage(msg);
			}
		};
		Message m = new Message();
		m.what = 0;
		h.sendMessageDelayed(m, 10000);	
		//On peut mettre ce que l'on veut. Vibreur, lecture d'un mp3 ou autre.
		//vibre pendant 1 seconde, puis attend 1/2 secondes avant de recommencer.
		//vibrator.vibrate(new long[]{0,1000,500,1000,2000}, -1);
		// ou vibre pendant 3 secondes.
		vibrator.vibrate(3000);
	}
}
